package cn.weekimwee.firstlineofcodeforkotlin.unit10

import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Binder
import android.os.IBinder
import android.support.v4.app.NotificationCompat
import android.util.Log
import cn.weekimwee.firstlineofcodeforkotlin.R

class MyService : Service() {

    private val binder = DownloadBinder()

    override fun onBind(intent: Intent): IBinder {
        return binder
    }

    override fun onCreate() {
        super.onCreate()
        Log.e("eeeee", "service -> onCreate")
        val intent = Intent(this,ServiceActivity::class.java)
        val pi = PendingIntent.getActivity(this,0,intent,0)
        val notification = NotificationCompat.Builder(this)
                .setContentTitle("this is content title")
                .setContentText("this is content text")
                .setWhen(System.currentTimeMillis())
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pi)
                .build()
        startForeground(1,notification)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.e("eeeee", "service -> onStartCommand")
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e("eeeee", "service -> onDestroy")
    }

    inner class DownloadBinder :Binder(){
        fun startDownload(){
            Log.e("eeeee","startDownload executed")
        }

        fun getProgress():Int{
            Log.e("eeeee","getProgress executed")
            return 0
        }
    }
}
